/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Apr 30, 2009

package com.sceneworker.editors.terraincreator;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.miginfocom.swing.MigLayout;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.util.GameTaskQueueManager;
import com.jmex.terrain.TerrainPage;
import com.jmex.terrain.util.AbstractHeightMap;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.TerrainAndHeightMapCacheObject;
import com.sceneworker.editors.terraincreator.heightmaps.HeightMapCreatorPanel;
import com.sceneworker.editors.terraincreator.info.TerrainDetailPanel;

public class TerrainCreator extends JDialog {

	private		HeightMapCreatorPanel		mcl_hMap;
	private		TerrainDetailPanel			mcl_detail;
	private		Node						mcl_parent;
	
	/**
	 * Create the dialog.
	 */
	public TerrainCreator(Node cl_par) {
		final	TerrainCreator		lcl_this = this;
		
		mcl_parent = cl_par;
		
		Container		lcl_contentPane = getContentPane();
		lcl_contentPane.setLayout(new MigLayout("inset 5"));

		JPanel		lcl_tabs = new JPanel(new MigLayout());
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		lcl_tabs.add(tabbedPane, "width 100%, height 100%");
		
		mcl_hMap = new HeightMapCreatorPanel();
		mcl_detail = new TerrainDetailPanel();
		tabbedPane.add("HeightMap", mcl_hMap);
		tabbedPane.add("Details", mcl_detail);		
		
		JButton cancelButton = SwingComponentFactory.makeAButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lcl_this.setVisible(false);
			}
		});
		
		JButton okButton = SwingComponentFactory.makeAButton("Create Terrain");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buildTerrain();
			}
			
		});
		
		lcl_contentPane.add(lcl_tabs, "width 100%, height 80%, wrap");
		MigLayoutUtils.makeRightAlignedButtonBar(lcl_contentPane, cancelButton, okButton);
		
		pack();
		setLocationRelativeTo(null);
	}
	
	
	private	void	buildTerrain() {
		try {
			// generate the height map
			final	AbstractHeightMap		lcl_hmap = mcl_hMap.generateHeightMap();
			
			final	String			lstr_name = mcl_detail.getTerrainName();
			final	int				li_blockSize = mcl_detail.getTerrainBlockSize();
			final	int				li_size = mcl_detail.getTerrainSize();
			final	Vector3f		lcl_scale = mcl_detail.getTerrainStepScale();
			
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					TerrainPage		lcl_terrain = new TerrainPage(lstr_name, li_blockSize, li_size, lcl_scale, lcl_hmap.getHeightMap());
					lcl_terrain.setModelBound(new BoundingBox());
					lcl_terrain.updateModelBound();
					lcl_terrain.updateRenderState();
					lcl_terrain.updateGeometricState(0, true);
					mcl_parent.attachChild(lcl_terrain);
					
					SceneWorkerGlobals.inst().addToCache(new TerrainAndHeightMapCacheObject(lcl_terrain, lcl_hmap));
					
					return null;
				}
				
			});
			
			setVisible(false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
